<?php

namespace api\modules\mini\controllers;

use api\controllers\OnAuthController;
use api\modules\mini\forms\spoker\SpokerForm;
use api\modules\mini\forms\user\UserFeedbackForm;
use api\modules\mini\forms\user\UserWithdrawAccountForm;
use common\helpers\CommonHelper;
use common\models\spoker\Spoker;
use services\spoker\SpokerService;
use services\user\UserFeedbackService;
use services\user\UserService;
use services\user\UserWithdrawAccountService;
use Yii;

/**
 * Class UserController
 * @package api\modules\mini\controllers
 */
class UserController extends OnAuthController
{
    public $modelClass = '';

    public function beforeAction($action)
    {
        $res = parent::beforeAction($action); // TODO: Change the autogenerated stub

        $user = Yii::$app->user->identity->user;
        if (in_array($action->id, ['create-spoker']) === false && $user && $user->spoker && $user->spoker->status != Spoker::STATUS_SUCCESS) {
            CommonHelper::api(422, '代言人未审核通过，无权限查看');
            return false;
        }
        return $res;
    }

    /**
     * @api {get} /mini/user/feedback-list 客户意见列表
     * @apiName feedback-list
     * @apiGroup MiniUser
     * @apiUse CommonParam
     *
     * @apiParam {Number} page                  页码
     * @apiParam {Number} pageSize              每页展示数
     *
     * @apiSuccess {Object} data            返回内容
     * @apiSuccess {Number} data.id         用户ID
     */
    public function actionFeedbackList()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $service = new UserFeedbackService();
        // 查询结果
        $data = $service->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化数据
        $data['list'] = $service->getListPageFormat($data['list']);
        return $data;
    }

    /**
     * @api {get} /mini/user/info 我的
     * @apiName info
     * @apiGroup MiniUser
     * @apiUse CommonParam
     *
     * @apiBody {String} mobile             姓名
     * @apiBody {String} avatar_url         头像
     *
     * @apiSuccess {Object} data            返回内容
     * @apiSuccess {Number} data.id         用户ID
     */
    public function actionInfo()
    {
//        $params = Yii::$app->request->get();

        $service = new UserService();
        return $service->myInfo();
    }

    /**
     * @api {get} /mini/user/statistics-data 我的【统计数据】
     * @apiName statistics-data
     * @apiGroup MiniUser
     * @apiUse CommonParam
     *
     * @apiParam {String} start_time        开始时间【格式：YYYY-MM-DD】
     * @apiParam {String} endt_time         结束时间【格式：YYYY-MM-DD】
     *
     * @apiBody {Number} patient_num        客户数
     * @apiBody {Number} arrive_num         到店数
     * @apiBody {Number} consume_money      销售额
     *
     * @apiSuccess {Object} data            返回内容
     * @apiSuccess {Number} data.id         用户ID
     */
    public function actionStatisticsData()
    {
        $params = Yii::$app->request->get();

        $service = new UserService();
        return $service->getUserStatisticsData($params);
    }

    /**
     * @api {get} /mini/user/withdraw-info 提现信息
     * @apiName withdraw-info
     * @apiGroup MiniUser
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                 提现ID【=0新增，>0编辑】
     * @apiBody {Number} type               类型【1支付宝、2银行】
     * @apiBody {String} card_no            卡号
     * @apiBody {String} name               姓名
     * @apiBody {String} bank_name          银行【type=2存在且必填】
     * @apiBody {Object} condition          筛选项
     * @apiBody {Array}  condition.type     类型
     * @apiBody {Number}  condition.type.is_show_bank 是否显示银行选项【1是、0否】
     *
     * @apiSuccess {Object} data            返回内容
     * @apiSuccess {Number} data.id         用户ID
     */
    public function actionWithdrawInfo()
    {
//        $params = Yii::$app->request->get();
        $service = new UserWithdrawAccountService();
        return $service->getMyInfo();
    }

    /**
     * @api {get} /mini/user/feedback-info 意见信息
     * @apiName feedback-info
     * @apiGroup MiniUser
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                 提现ID【=0新增，>0编辑】
     * @apiBody {Number} type               类型【1支付宝、2银行】
     * @apiBody {String} card_no            卡号
     * @apiBody {String} name               姓名
     * @apiBody {String} bank_name          银行【type=2存在且必填】
     * @apiBody {Object} condition          筛选项
     * @apiBody {Array}  condition.type     类型
     * @apiBody {Number}  condition.type.is_show_bank 是否显示银行选项【1是、0否】
     *
     * @apiSuccess {Object} data            返回内容
     * @apiSuccess {Number} data.id         用户ID
     */
    public function actionFeedbackInfo()
    {
//        $params = Yii::$app->request->get();
        $service = new UserFeedbackService();
        return $service->getInfo();
    }

    /**
     * @api {post} /mini/user/create-spoker 新增代言人
     * @apiName create-spoker
     * @apiGroup MiniUser
     * @apiUse CommonParam
     *
     * @apiBody {String} name               姓名
     * @apiBody {String} mobile             手机号
     * @apiBody {String} referrer_name      推荐人
     *
     * @apiSuccess {Object} data            返回内容
     * @apiSuccess {Number} data.id         用户ID
     */
    public function actionCreateSpoker()
    {
        $params = Yii::$app->request->post();

        $model = new SpokerForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            return CommonHelper::api(422, CommonHelper::analyErr($model->getFirstErrors()));
        }

        $service = new SpokerService();
        $service->create($model);
        return true;
    }

    /**
     * @api {post} /mini/user/edit-withdraw 编辑提现账号
     * @apiName edit-withdraw
     * @apiGroup MiniUser
     * @apiUse CommonParam
     *
     * @apiBody {String} type               类型【1支付宝、2银行】
     * @apiBody {String} card_no            卡号
     * @apiBody {String} name               姓名
     * @apiBody {String} bank_name          银行
     *
     * @apiSuccess {Object} data            返回内容
     * @apiSuccess {Number} data.id         用户ID
     */
    public function actionEditWithdraw()
    {
        $params = Yii::$app->request->post();

        $model = new UserWithdrawAccountForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            return CommonHelper::api(422, CommonHelper::analyErr($model->getFirstErrors()));
        }

        $service = new UserWithdrawAccountService();
        $service->edit($model);
        return true;
    }

    /**
     * @api {post} /mini/user/create-feedback 提交意见
     * @apiName create-feedback
     * @apiGroup MiniUser
     * @apiUse CommonParam
     *
     * @apiBody {Number} patient_id         患者ID
     * @apiBody {String} content            意见
     *
     * @apiSuccess {Object} data            返回内容
     * @apiSuccess {Number} data.id         用户ID
     */
    public function actionCreateFeedback()
    {
        $params = Yii::$app->request->post();

        $model = new UserFeedbackForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            return CommonHelper::api(422, CommonHelper::analyErr($model->getFirstErrors()));
        }

        $service = new UserFeedbackService();
        $service->create($model);
        return true;
    }

}